﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using DBLayer;

namespace BALayer
{
    public class KhachHang
    {
        private DAL db = null;

        public KhachHang()
        {
            db = new DAL();
        }

        public DataSet DanhSachKhachHang()
        {
            // id, hoten, diachi, sdt
            return db.ExecuteQueryDataSet("uspDanhSachKhachHang", CommandType.StoredProcedure, null);
        }

        public int ThemKhachHang(ref string error,String hoten,String diachi,String sdt)
        {
            SqlParameter maKH = new SqlParameter("@Id", DbType.Int32);
            maKH.Direction = ParameterDirection.Output;
            int makhachhang = -1;

            if(db.ExecuteNonQuery("uspThemKhachHang", CommandType.StoredProcedure, ref error,
                new SqlParameter("@Hoten", hoten),
                new SqlParameter("@Diachi", diachi),
                new SqlParameter("@Sdt", sdt),
                maKH))
            {
                makhachhang = Convert.ToInt32(maKH.Value);
            }

            return makhachhang;
        }

        public bool CapNhaKhachHang(ref string error,int id,String hoten,String diachi,String sdt)
        {
            return db.ExecuteNonQuery("uspCapNhatKhachHang", CommandType.StoredProcedure, ref error,
                new SqlParameter("@Id", id),
                new SqlParameter("@Hoten", hoten),
                new SqlParameter("@Diachi", diachi),
                new SqlParameter("@Sdt", sdt));
        }

        public bool XoaKhachHang(ref string error,String id)
        {
            return db.ExecuteNonQuery("uspXoaKhachHang", CommandType.StoredProcedure, ref error,
                new SqlParameter("@Id", id));
        }
    }
}
